package com.itheima.app.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.im.HuanXinTemplate;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserCommentVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class MessageManager {

    @DubboReference
    UserInfoService userInfoService;

    @DubboReference
    QuestionService questionService;

    @Autowired
    HuanXinTemplate huanXinTemplate;

    @DubboReference
    FriendService friendService;

    @DubboReference
    CommentService commentService;


    public void replyStrangerQuestions(Long recommendUserId, String reply) {
        //1.获取当前登陆者
        Long userId = UserHolder.get().getId();

        //2.获取当前登陆者的userInfo
        UserInfo userInfo = userInfoService.findByUserId(userId);

        //3.获取推荐用户的陌生人问题
        Question question = questionService.findByUserId(recommendUserId);
        //注意:对方有可能没有设置问题,若没有就使用默认问题
        if (question == null) {
            question = new Question();
        }

        //4.封装回复信息
        /*
            userId:登陆者id
            huanXinId:登陆者环信id
            nickname:登陆者昵称
            strangerQuestion:推荐用户的陌生人问题
            reply:登陆者回复的内容
         */
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("huanXinId","HX"+userId);
        map.put("nickname",userInfo.getNickname());
        map.put("strangerQuestion",question.getStrangerQuestion());
        map.put("reply",reply);

        //5.调用环信模块发送信息
        huanXinTemplate.sendMsg("HX"+recommendUserId, JSON.toJSONString(map));
    }


    // 添加好友
    public void addContact(Long friendId) {
        //1.获取当前登陆者id
        Long userId = UserHolder.get().getId();

        //2.调用friendService添加好友
        friendService.addContact(userId,friendId);

        //3.调用环信模版添加好友
        huanXinTemplate.addContacts("HX"+userId,"HX"+friendId);
    }


    public ResponseEntity findCommentsByPage(Integer pageNum, Integer pageSize, int commentType) {
        //1.获取登陆者id
        Long publishUserId = UserHolder.get().getId();

        //2.调用service查询当前页的数据
        PageBeanVo pageBeanVo = commentService.findByPage(publishUserId, commentType, pageNum, pageSize);

        //3.获取pageBeanVo中的list(操作人id)
        List<Comment> items = (List<Comment>) pageBeanVo.getItems();

        //4.遍历集合,获取每个操作人的userInfo,将其封装成UserCommentVo,将每个vo放入voList中
        List<UserCommentVo> voList = new ArrayList<>();

        if (CollUtil.isNotEmpty(items)) {
            items.forEach(comment -> {
                UserInfo userInfo = userInfoService.findByUserId(comment.getUserId());

                UserCommentVo vo = new UserCommentVo();
                vo.setId(userInfo.getId().toString());
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickname(userInfo.getNickname());
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated())));

                voList.add(vo);
            });
        }

        //5.给pageBeanVo设置volIst且返回
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }
}
